package de.gameduell.net.src.body {
	
	import de.gameduell.net.src.encryption.IEncryption;
	import de.gameduell.net.src.hashing.IHashing;	

	public class BodyFactory {

		public static const V2:int = 2;

		public function createBody(version:int,requestType:String,parameters:Array,hashing:IHashing,encryption:IEncryption):IBody {
			switch(version) {
				case(V2): 	
					return new Body_V2(requestType, parameters, hashing, encryption);
				default: 	
					return null;
			}
		}

		public function extractBodies(version:int,data:String,hashing:IHashing,encryption:IEncryption):Array {
			switch(version) {
				case(V2):
					var bodyData:Array = encryption.decrypt(data).split("\n");
					var bodies:Array = new Array();
					for(var i:int = 0;i < bodyData.length; i++) {
						var parameters:Array = bodyData[i].split("\t");
						if(i == 0) {
							// remove hash
							parameters.shift();
						}
						var requestType:String = parameters.shift();
						bodies.push(new Body_V2(requestType, parameters, hashing, encryption));
					}
					return bodies;
				default: 
					return null;
			}
		}
	}
}
